#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QtSerialPort/QSerialPort>
#include <QMessageBox>
#include <QSettings>  //读写ini文件
#include <QFile>
#include "options.h"
#include <QtMath>
#include "qcustomplot.h"

#define V_REAL_CHA  101  // 101-104为CHA\CHB的浓度值地址,共4字节
#define MSA9020_COUNT 10 // 9020卡槽的数量

#pragma pack(push,1)  // 字节对齐
union func03_tx {
    struct {
        quint8 slaveAddress;
        quint8 funcCode;
        quint16 address; //内存地址
        quint16 number;
        quint16 crc;
    } msa;
    char data[8];
};

union func03_rx {
    struct {
        quint8 slaveAddress;
        quint8 funcCode;
        quint8 number;
        quint16 CHA_value;   //A实际浓度值
        quint16 CHA_decimals;//A小数点位数
        quint16 CHB_value;
        quint16 CHB_decimals;
        quint16 crc;
    } msa;
    char data[13];
};

union func07_tx {
    struct {
        quint8 slaveAddress;
        quint8 funcCode;
        quint16 crc;
    } msa;
    char data[4];
};

union func07_rx {
    struct {
        quint8 slaveAddress;
        quint8 funcCode;
        quint8 returnValue;
        quint16 crc;
    } msa;
    char data[5];
};
#pragma pack(pop)   // 字节对齐END

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();



private slots:
    void time_update(); //时间更新槽函数，状态栏显示时间
    void readMyCom();
    void on_pushButton_clicked();
    void on_action_openCom_triggered();
    void on_action_closeCom_triggered();
    void on_action_options_triggered();
    void receiveData();   //接收传递过来的数据的槽

private:
    Ui::MainWindow *ui;
    QTimer *timer;
    QSerialPort *myCom;
    QSettings configIni;
    QLabel *ComStatic;
    QFile logTxt;
    QPointer<QCPGraph> mGraphA[MSA9020_COUNT], mGraphB[MSA9020_COUNT];

    int modaddr[MSA9020_COUNT]; // 存储10个卡槽的modbus地址
    int modCount[MSA9020_COUNT]; // 串口发送、接收计数，用来判断通讯状态
    int SendIndex = 1;   // 记录串口向哪个卡槽发送命令
    union func03_rx rx03;
    union func07_rx rx07;
    union func03_tx tx03;
    union func07_tx tx07;
    bool openCom();
    void closeCom();
    void initfrom();
    void disp03(union func03_rx rx03);
    void disp07(union func07_rx rx07);
    void setAlarmStatus(QString objName, bool status);
    void send03to(int index); // 向index号卡槽发送03命令
    void send07to(int index); // 向index号卡槽发送07命令
    void writeLog(QString w_r, char *buf, int len);

};


#endif // MAINWINDOW_H
